<?php

namespace App\Admin\Controllers;

use App\Models\TradeActivities;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class TradeActivitiesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('版贸书讯');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('版贸书讯');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('版贸书讯');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::script("popverLoad();");
        return Admin::grid(TradeActivities::class, function (Grid $grid) {

            $grid->disableExport();
            $grid->id('ID')->sortable();
            $grid->disableExport();

            $grid->column('title', '活动名称');
            $grid->column('image', '专题图')->display(function ($value) {
                $url = asset('uploads/' . $value);

                return '
                    <a class="layui-btn layui-btn-primary product-popver" href="javascript:;" src-data="' . $url . '">预览</a>
                    <div id="popover-img-modal" class="hide layui-layer-wrap" style="display: block;">
                        <img id="popover-modal-img" src="'. $url .'">
                    </div>
                ';
            });
            $states = [
            	'on'  => ['value' => '1', 'text' => '已推荐', 'color' => 'success'],
            	'off' => ['value' => '0', 'text' => '否', 'color' => 'danger'],
            ];
            $grid->slideshow('推荐到轮播图')->switch($states);

            $grid->column('link', '链接')->display(function ($value) {
                return '<a href="' . $value . '" target="_blank">查看</a>';
            });

            $grid->column('date_time', '活动时间');
            $grid->created_at('上线时间');

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like('title', '活动名称');
                $filter->between('date_time', '活动时间')->date();
                $filter->between('created_at', '上线时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        set_time_limit(600);
        return Admin::form(TradeActivities::class, function (Form $form) {
            $form->text('title', '活动名称')->rules('required');
            $form->textarea('summary', '活动简介')->rules('required');
            $states = [
            		'on'  => ['value' => '1', 'text' => '已推荐', 'color' => 'success'],
            		'off' => ['value' => '0', 'text' => '否', 'color' => 'danger'],
            ];
            $form->switch('slideshow','是否推荐到轮播图')->states($states)->default('0');
            $form->date('date_time', '活动时间')
                ->placeholder('输入活动时间')
                ->rules('required');
            $form->image('image', '活动大图')
                ->removable()
                ->attribute('accept', 'image/png, image/jpeg, image/jpg')
                ->help('建议尺寸258 * 366，不大于5Mb，支持格式：png、jpeg、jpg')
                ->rules('required|max:5120');
            $form->url('link', '活动链接')->rules('required');
            $form->display('created_at', '添加时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
